/*==============================================================================
FILE NAME: Figure_A2.do
CREATED: 13 June 2025
==============================================================================*/

**Figure A2

/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/

//12 months of lags, any air investigation

// Load in data
use "$processed_data/Air_Panel.dta", clear

// Drop if there was never an air investigation
drop if never_air_inv == 1

// Create panel time variable
egen t = group(year month)
xtset RN_id t

// Generate variables for outcomes

forv h = 0/12 {
	gen p_air_inv_`h' = f`h'.p_air_inv - l1.p_air_inv
}

// Generate variables for placebo
forv h = 2/12 {
	gen p_air_inv_neg`h' = l`h'.p_air_inv - l1.p_air_inv
}
// Create a unit-year fixed effect variable
egen RN_year = group(RN year)

// Initialize containers for regression results
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0

// Run regressions for 0 to 12 months after incident
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_inv_`h'  l(1/12)p_air_incident p_air_incident, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_incident] if Years == `h'
replace se = _se[p_air_incident] if Years == `h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == `h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == `h'
}

// Run regressions for 2 to 12 months before incident (placebo effects)
foreach h in  2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_inv_neg`h' l(1/12)p_air_incident p_air_incident, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_incident] if Years == -`h'
replace se = _se[p_air_incident] if Years == -`h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == -`h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == -`h'
}

// Prepare data for plotting
preserve
keep if Years != .
keep b u d se Years Zero

// Plot figure
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(vlarge)) ylabel(-0.2(0.1)0.6,labsize(vlarge)) legend(off) ytitle("{&Delta} P(Investigation)", size(vlarge)) xtitle("Months", size(vlarge)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)

// Save figure
graph export "$figures/Figure_A2.pdf", replace